package com.flowable.service.impl;

import com.flowable.service.ResourceService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * 部署相关 实现
 */
@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 读取已部署到flowable中的xml文件
     *
     * @param processDefinitionId
     * @param resType
     * @return
     * @throws Exception
     */
    @Override
    public InputStream resourceRead(String processDefinitionId, String resType) throws Exception {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().
                processDefinitionId(processDefinitionId)
                .singleResult();
        String resourceName = "";
        if (resType.equals("image/png")) {
            resourceName = processDefinition.getDiagramResourceName();
        } else if (resType.equals("text/xml")) {
            resourceName = processDefinition.getResourceName();
        }
        InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
        return resourceAsStream;
    }
}
